<template>
  <div class="training-plan-container">
    <h2 class="page-title">我的培训计划</h2>

    <!-- 培训计划信息 -->
    <div v-if="trainingPlan" class="training-plan-card-container">
      <el-card class="training-plan-card" shadow="hover" :body-style="{ padding: '20px' }">
        <!-- 封面图片 -->
        <div class="plan-image">
          <img :src="trainingPlan.img" alt="培训计划封面" class="plan-img" />
        </div>

        <!-- 计划详情 -->
        <div class="training-plan-details">
          <div><strong>培训计划名称:</strong> {{ trainingPlan.name }}</div>
          <div><strong>描述:</strong> {{ trainingPlan.description }}</div>
          <div><strong>开始日期:</strong> {{ trainingPlan.startDate }}</div>
          <div><strong>结束日期:</strong> {{ trainingPlan.endDate }}</div>
        </div>

        <!-- 学习进度条 -->
        <el-progress :percentage="progress" status="success" :show-text="true"></el-progress>
        <div v-if="progress === 100" class="completion-message">
          <el-message type="success" title="课程已完成" />
        </div>

        <!-- 按钮容器，使用 Flexbox 对齐按钮 -->
        <div class="button-container">
          <!-- 学习按钮 -->
          <el-button type="primary" class="start-learning-btn" @click="startLearning">进入学习</el-button>

          <!-- 退选按钮 -->
          <el-button type="danger" class="withdraw-btn" @click="withdrawTrainingPlan">退选培训计划</el-button>
        </div>
      </el-card>
    </div>

    <!-- 无培训计划时的提示 -->
    <div v-else class="no-training-plan">
      <p>未选择培训计划或培训计划信息不完整</p>
    </div>
  </div>
</template>
<script setup>
import { ref, onMounted } from 'vue';
import axios from '@/utiles/axios';
import { ElMessage } from 'element-plus';
import { useRouter } from 'vue-router';

const trainingPlan = ref(null);  // 存储选择的培训计划
const progress = ref(0);         // 存储学习进度
const studentId = localStorage.getItem('studentId');  // 获取当前学员ID
const router = useRouter();  // 用于跳转到任务点列表页面

// 获取学员的培训计划
const getTrainingPlan = async () => {
  try {
    const studentResponse = await axios.get(`/student/info?id=${studentId}`);
    const trainingPlanId = studentResponse.data.trainingPlan?.id;
    if (!trainingPlanId) {
      throw new Error('未找到培训计划 ID');
    }

    const courseResponse = await axios.get(`/student/${studentId}/trainingPlan`);
    trainingPlan.value = courseResponse.data;

  } catch (error) {
    console.error('获取培训计划失败', error);
    ElMessage.error('获取培训计划失败');
  }
};



// 点击“进入学习”按钮后跳转到任务点列表页面
const startLearning = () => {
  const trainingPlanId = trainingPlan.value.id;
  router.push(`/task/${trainingPlanId}`);  // 跳转到任务点列表页面，带上培训计划ID
};

// 退选培训计划
const withdrawTrainingPlan = async () => {
  try {
    const response = await axios.put(`/student/${studentId}/withdraw`);
    ElMessage.success('退选成功');
    trainingPlan.value = null;  // 清空当前培训计划
    progress.value = 0;         // 清空学习进度
  } catch (error) {
    console.error('退选失败', error);
    ElMessage.error('退选失败');
  }
};

onMounted(() => {
  getTrainingPlan();  // 页面加载时获取学员选择的培训计划
});
</script>
<style scoped>
/* 页面容器样式 */
.training-plan-container {
  padding: 20px;
  background-color: #f8f8f8;
  min-height: 100vh;
}

/* 页面标题 */
.page-title {
  text-align: center;
  font-size: 26px;
  font-weight: bold;
  color: #409EFF;
  margin-bottom: 30px;
}

/* 卡片容器样式 */
.training-plan-card-container {
  max-width: 800px;
  margin: 0 auto;
}

/* 培训计划卡片样式 */
.training-plan-card {
  border-radius: 12px;
  background-color: #fff;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
}

/* 封面图片样式 */
.plan-image {
  width: 100%;
  height: 200px;
  border-radius: 12px;
  overflow: hidden;
}

.plan-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* 培训计划详情 */
.training-plan-details {
  padding: 20px;
  font-size: 16px;
  color: #333;
}

.training-plan-details div {
  margin-bottom: 15px;
}

.training-plan-details strong {
  color: #409EFF;
}

/* 学习进度条样式 */
.el-progress {
  margin-top: 20px;
}

/* 按钮容器样式 */
.button-container {
  display: flex;
  gap: 20px;  /* 设置按钮间距 */
  margin-top: 20px;
  justify-content: space-between;
}

/* 学习按钮样式 */
.start-learning-btn {
  width: 48%;  /* 设置按钮宽度 */
  padding: 15px;
  font-size: 16px;
  background-color: #409EFF;
  border-color: #409EFF;
  border-radius: 8px;
  transition: background-color 0.3s;
}

.start-learning-btn:hover {
  background-color: #66b1ff;
  border-color: #66b1ff;
}

/* 退选按钮样式 */
.withdraw-btn {
  width: 48%;  /* 设置按钮宽度 */
  padding: 15px;
  font-size: 16px;
  background-color: #f56c6c;
  border-color: #f56c6c;
  border-radius: 8px;
  transition: background-color 0.3s;
}

.withdraw-btn:hover {
  background-color: #f79c9c;
  border-color: #f79c9c;
}

/* 空培训计划时的提示 */
.no-training-plan {
  text-align: center;
  font-size: 18px;
  color: #999;
  margin-top: 50px;
}

/* 完成提示 */
.completion-message {
  margin-top: 20px;
  text-align: center;
  font-size: 18px;
  color: #409EFF;
}
</style>